<?php
/**
 * 获取指定店铺的订单
 * 
 * @author hzjsq@msn.com
 * @version 01.
 */
//$argv[1] = 'dev.fh.taoex.com';
//$argv[2] = '2011-8-1 00:00:00';
//$argv[3] = '2011-8-2 23:59:59';

$domain = $argv[1];
$sDate = $argv[2];
$eDate = $argv[3];


define('COMMAND_MODE', true);

set_time_limit(0);

define('LIB_DIR', dirname(__FILE__) . '/../lib/'); 
require_once(LIB_DIR . 'api/taobao/order.php');
require_once(dirname(__FILE__) . '/../lib/init.php');

$orderObj = new tools_api_taobao_order();
$init = kernel::single('omestart_init');
$init->startRun();

$shopList = getShopList();
//$shopList = json_decode('[{"shop_id":"0368b91c0192ba906972e0da48d07798","shop_bn":"13116935244e2edad49b","name":"\u590f\u82b1\u821e Dance","shop_type":"taobao","config":{"url":"http:\/\/shop60857247.taobao.com","account":"\u604b\u821e_2009","password":""},"last_download_time":"1311859242","last_upload_time":null,"active":"true","disabled":"false","last_store_sync_time":null,"area":"mainland:\u5e7f\u4e1c\/\u6df1\u5733\u5e02\/\u7f57\u6e56\u533a:525","zip":"518000","addr":"\u4e1c\u95e8\u4e2d\u8def \u5357\u6d77\u4e2d\u5fc3 B\u5ea7 9E","default_sender":"\u590f\u82b1\u821e\u670d\u9970Dance","mobile":"13715110055","tel":"82180349","filter_bn":"false","bn_regular":null,"express_remark":null,"delivery_template":null,"order_bland_template":null,"node_id":"","node_type":"taobao","addon":{"session":"40726166d5e25d5226b401ewC1ZGyJZfbd3a4e6fb9a97352748748781","nickname":"\u604b\u821e_2009"}}]', true);

$count = 0;
foreach ((array) $shopList as $key => $shop) {
    
    if ($shop['addon']['session']) {
        
        $orderObj->setSessionKey($shop['addon']['session']);
        $orders = $orderObj->fetch($sDate, $eDate);
        
        $shopList[$key]['orders']['order'] = $orders;
        $shopList[$key]['orders']['count'] = count($orders);
        $count = $shopList[$key]['orders']['count'] + $count;
    }
} 

$init->updateInfo(array('count' => $count, 'process' => $runed));

$runed = 0;
foreach ((array) $shopList as $key => $shop) {
    
        foreach ((array)$shop['orders']['order'] as $tid) {

            $orderOme = $orderObj->getFullTrade($tid);

            if (!empty($orderOme)) {

                $result = insertIntoOrder($orderOme, $shop['node_id']);
            }
            
            ilog("{$tid} done.");
            $runed ++;
            $init->updateInfo(array('count' => $count, 'process' => $runed));
        }
}

$init->finish();

function insertIntoOrder($sdf, $nodeId) {
    
    static $orderObj = null;
    static $response = null;
    
    if (!$orderObj) {
        $orderObj = new ome_rpc_response_order();
        $response = kernel::single('base_rpc_service');
    }
    
    base_rpc_service::$node_id = $nodeId;
    
    return $orderObj->add($sdf, $response);
}

function getShopList() {
    
    $db = kernel::database();
    $list = $db->select("SELECT * FROM sdb_ome_shop WHERE disabled='false'");
    if ($list) {
        foreach($list as $key => $item) {
            
            $list[$key]['addon'] = unserialize($item['addon']);
            $list[$key]['config'] = unserialize($item['config']);
        }
        return $list;
    } else {
        
        return array();
    }
}

/**
 * 日志
 */
function ilog($str) {
	
	global $domain;
        $path = dirname(__FILE__) . '/../logs/init/';
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
	$filename = dirname(__FILE__) . '/../logs/init/'.$domain . '.log';
	$fp = fopen($filename, 'a');
	fwrite($fp, date('Y-m-d H:i:s') . ' : ' . $str . "\n");
	fclose($fp);
}